﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace Fengsoft.AIBlog.Web.Code
{
    public static class CultureHelper
    {
        // Include only cultures you are implementing as views.
        private static readonly Dictionary<String,bool> _cultures=new Dictionary<string,bool>() 
        {
            {"en-US",false}, // The first culture is the default.
            {"th-TH",false}
        };
        /// <summary>
        /// Returns default culture name which is the first name decalared (e.g. en-US).
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultCulture()
        {
            return _cultures.Keys.ElementAt(0); // Return default culture.
        }
        public static List<CultureInfo> GetSupportedCultures()
        {
            List<CultureInfo> result=new List<CultureInfo>(_cultures.Count);
            foreach (KeyValuePair<string,bool> entry in _cultures)
            {
                result.Add(CultureInfo.CreateSpecificCulture(entry.Key));
            }
            return result;
        }
        /// <summary>
        /// Returns a valid culture name based on "name" parameter. If "name" is not valid, it returns the default culture "en-US".
        /// </summary>
        /// <param name="name">Culture's name (e.g. en-US).</param>
        /// <returns></returns>
        public static string GetValidCulture(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return GetDefaultCulture(); // Return default culture.
            }
            if (_cultures.ContainsKey(name))
            {
                return name;
            }
            // Find a close match. For example, if you have "en-US" defined and the user requests "en-GB", 
            // the function will return closes match that is "en-US" because at least the language is the same (ie English)            
            foreach (var c in _cultures.Keys)
            {
                if (c.StartsWith(name.Substring(0,2)))
                {
                    return c;
                }
            }
            return GetDefaultCulture(); // Return default culture as no match found.
        }
        /// <summary>
        /// Returns "true" if view is implemented separatley, and "false" if not. For example, if "es-CL" is true, then separate views must exist e.g. Index.es-cl.cshtml, About.es-cl.cshtml.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool IsViewSeparate(string name)
        {
            bool result=false;
            if (_cultures.ContainsKey(name))
            {
                result=_cultures[name];
            }
            return result;
        }
    }
}